# Maintainer: Antonio Rojas <arojas@archlinux.org>
# Contributor: Ronald van Haren <ronald.archlinux.org>
# Contributor: Damir Perisa <damir@archlinux.org>

pkgbase=maxima
pkgname=($pkgbase{,-sbcl,-ecl,-fas})
pkgver=5.46.0
_sbclver=2.3.3
_eclver=21.2.1
pkgrel=9
pkgdesc='A sophisticated computer algebra system'
arch=(x86_64)
license=(GPL)
url='http://maxima.sourceforge.net'
depends=(texinfo shared-mime-info)
makedepends=(python emacs ecl sbcl)
# needs rebuild when bash changes version
# needs a rebuild when ecl or sbcl changes version
options=(!zipman) # don't zip info pages or they won't work inside maxima
source=(https://downloads.sourceforge.net/sourceforge/$pkgname/$pkgname-$pkgver.tar.gz
        matrixexp.patch
        maxima-sbcl-gmp.patch)
sha256sums=('7390f06b48da65c9033e8b2f629b978b90056454a54022db7de70e2225aa8b07'
            'ef1bc6a15fc982ff8c6aa1800bbbd3284d9e060ca27abf9d8c1266632c0c2619'
            '7e85da0b6672b096d7aefac861573a57323dfe805e08d033781448f7ca6ed856')

prepare() {
  patch -d $pkgname-$pkgver -p1 < matrixexp.patch # fix matrix exponentiation
  patch -d $pkgname-$pkgver -p1 < maxima-sbcl-gmp.patch # Use GMP arithmetic with sbcl (Void Linux)
}

build() {
  cd $pkgbase-$pkgver
  ./configure \
    --prefix=/usr \
    --mandir=/usr/share/man \
    --infodir=/usr/share/info \
    --libexecdir=/usr/lib \
    --enable-sbcl \
    --enable-ecl \
    --with-default-lisp=sbcl

  # help avoid (re)running makeinfo/tex
  touch doc/info/maxima.info
  make
}

#check() {  # test 23&129 fail from rtest_taylor
#  cd $pkgname-$pkgver
#  make check
#}

package_maxima() {
  depends+=(maxima-backend)
  replaces=('maxima-ecl<5.45.1-9')
  optdepends=('gnuplot: plotting capabilities'
              'rlwrap: readline support via /usr/bin/rmaxima'
              'tk: graphical xmaxima interface')

  cd $pkgbase-$pkgver
  make DESTDIR="$pkgdir" emacsdir=/usr/share/emacs/site-lisp/maxima install

# Remove backends
  rm -r "$pkgdir"/usr/lib/maxima/$pkgver/binary-*
}

package_maxima-sbcl() {
  pkgdesc='SBCL backend for Maxima'
  depends=(gmp sbcl=$_sbclver)
  provides=(maxima-backend)

  install -Dm755 $pkgbase-$pkgver/src/binary-sbcl/maxima.core -t "$pkgdir"/usr/lib/maxima/$pkgver/binary-sbcl
}

package_maxima-ecl() {
  pkgdesc='ECL backend for Maxima'
  depends=(ecl=$_eclver)
  provides=(maxima-backend)

  install -Dm755 $pkgbase-$pkgver/src/binary-ecl/maxima -t "$pkgdir"/usr/lib/maxima/$pkgver/binary-ecl
}

package_maxima-fas() {
  pkgdesc='Maxima FAS module for ECL'
  depends=(ecl=$_eclver maxima)

  install -Dm644 $pkgbase-$pkgver/src/binary-ecl/maxima.fas -t "$pkgdir"/usr/lib/ecl-$_eclver
}
